package lesson.ch11;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MainApp {
	static StudentMapper stuMapper;

	public static void main(String[] args) {
		String resource = "config.xml";
		System.out.println(MainApp.class.getResource(resource).getFile());
		InputStream is = MainApp.class.getResourceAsStream(resource);

		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);

		SqlSession session = sessionFactory.openSession();

		stuMapper = session.getMapper(StudentMapper.class);
		// String name,int age,int sex,String address
		stuMapper.deletAll();
		listTab();
		Student stu1 = new Student("nick", 32, 1, "shenzhen");
		stuMapper.insert(stu1);
		stuMapper.insert(stu1);
		stuMapper.insert(stu1);
		stuMapper.insert(stu1);
		stuMapper.insert(stu1);
		stuMapper.insert(stu1);
		listTab();
		stuMapper.deleteStuById(41);
		listTab();
		session.commit();
		session.close();
	}

	public static void listTab() {
		System.out.println("=============================");
		for (Student stu : stuMapper.getStus()) {
			System.out.println(stu);
		}
	}

}
